package com.zle.service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.zle.dao.IpersonDao;
import com.zle.dao.bean.Person;

@Service
public class DeviceService implements IdeviceService{
	@Autowired
	IpersonDao persionDao;
	/**
	 * 分页
	 */
	public void queryPerson(){
		Pageable pageable = new PageRequest(1, 10);
		Page<Person> findAll = persionDao.findAll(pageable);
		findAll.getTotalElements();
		findAll.getContent();
	}
	/**
	 * 分页+排序
	 */
	public void queryPersonAndSort(){
		//按照id倒序排列
		Order order = new Order(Sort.DEFAULT_DIRECTION.DESC,"id");
		Sort sort = new Sort(order);
		Pageable pageable = new PageRequest(1, 10,sort);
		Page<Person> findAll = persionDao.findAll(pageable);
		findAll.getTotalElements();
		findAll.getContent();
	}
	/**
	 * 分页+排序+条件
	 */
	public void queryPersonAndSortAndWhere(){
		//按照id倒序排列
		Order order = new Order(Sort.DEFAULT_DIRECTION.DESC,"id");
		Sort sort = new Sort(order);
		Pageable pageable = new PageRequest(1, 10,sort);
		
		Specification<Person> spe = new Specification<Person>() {
			
			@Override
			public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Path path = root.get("age");
				
				return cb.gt(path, 100);
			}
		};
		
		Page<Person> findAll = persionDao.findAll(spe,pageable);
		findAll.getTotalElements();
		findAll.getContent();
	}
	
	
	@Override
	public boolean register(String devName,String bind,String sn){
		//TODO 测出持久化
		if(bind.startsWith("0000-")){
			return false;
		}
		return true;
	}

	@Override
	public boolean unRegister(String devid) {
		
		return true;
	}
	

}
